<?php

namespace app\admin\controller\union;
/* 女用户每日收益 */

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\union\Guild;
use app\common\controller\Backend;
use app\admin\model\union\GuildHostAccountDaily;

class Hostshare extends Backend
{
    protected $model = null;
    protected $searchFields = '';
    protected $admin_id = null;
    protected $guild_id = 0;
    protected $guild_manage_id = 0;
    public function _initialize()
    {
        parent::_initialize();
        //获取后台登录管理员id
        $auth = Auth::instance();
        $this->admin_id = $auth->isLogin() ? $auth->id : 0;
        $this->assign('admin_id', $this->admin_id);
        $this->guild_id = Admin::get(intval($auth->id))['guild_id'];
//        if($this->guild_id)$this->guild_manage_id = Guild::where('guild_id',$this->guild_id)->value('id');
        $this->model = new GuildHostAccountDaily();

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $child_data = Guild::getChildGuild($this->guild_id);
            $field = "u.user_id,u.user_profile_id,u.host_share,u.nick_name,g.* ";
            $list = $this->model->field($field)
                ->alias('g')
                ->join('zy_voice_call.t_user_profile u',"u.user_id=g.host_id","LEFT")
                ->where($where);
//            if (!empty($child_data)){
//                $child_id = array_keys($child_data);
//                array_push($child_id,$this->guild_id);
//                $list = $list->where('guild_manage_id','in',$child_id);
//            }else{
                $where2['guild_manage_id'] = $this->guild_id;
                $list = $list->where($where2);
//            }
//            if ($this->guild_id){
//                $where2['guild_manage_id'] = $this->guild_id;
//                $list = $list->where($where2);
//            }
            $list = $list->order($sort, $order)
                ->paginate($limit);

            //推荐人名称
            if ($this->guild_id){
                $guild_row = Guild::field('id,name')->where('id',$this->guild_id)->find()->toArray();
            }else{
                $guild_row['name'] = '';
            }

            foreach ($list->items() as $v){
                if ($this->guild_id == $v->guild_manage_id){
                    $v->guild_name = $guild_row['name'];
                }else if (!empty($child_data[$v->guild_manage_id])){
                    if ($this->guild_id == 0){
                        $v->guild_name = $child_data[$v->guild_manage_id];
                    }else{
                        $v->guild_name = $guild_row['name'].'-'.$child_data[$v->guild_manage_id];
                    }
                }else{
                    $v->guild_name = '';
                }
                if ($v->total_turnover > 0){
                    $v->total_share_income = round(($v->total_turnover) * ($v->host_share),2);
                }else{
                    $v->total_share_income = 0;
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $select = Guild::getChildGuild2($this->guild_id);

        $this->assign('select',$select);
        return $this->view->fetch();
    }
}